$(function() {
	new Vue({
		el: '#addHotPicApp',
		data: {
			fullscreenLoading: false,
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			fileList: [],
			form: {
				showOrder:0,
			},
			goodPicTable:[],
			value: "请选择商品分类",
			districtDefault: [],
			options: [],
			uploadForm: {
			},
		},
		created: function() {
			parent.vueData.$data.hreHtml = "../addHomeCover/index.html";
			parent.vueData.activeIndex = "6";
			parent.vueData.getLeftNav("6")
			parent.vueData.$data.categoryIndex = 4;
			this.getAllCategory();
			this.getAllCoverList();
		},
		methods: {
			//删除
			beforeRemove(file, fileList) {
				let index = this.fileList.findIndex(fileItem => {
					return fileItem.url === file.url
				})
				this.fileList.splice(index, 1)
				return true;
			},
			//上传
			handleImgSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.form.photo = response.result
					this.fileList.push({
						name: "首页轮播图",
						url: response.result,
					})
				}
			},
			isNotNumber(inputData) {
				if (parseFloat(inputData).toString() == "NaN") {
					return false;
				} else {
					return true;
				}
			},
			handleImgExceed(files, fileList) {
				this.$message.warning("首页轮播图一次只能上传一张图片")
			},
			configHotPic() {
				if (this.fileList.length < 1) {
					this.$message.error("首页轮播图不能为空");
					return;
				}
				if(stringEmpty(this.uploadForm.categoryId)){
					this.$message.error("商品分类不能为空");
					return;
				}
				if(!this.isNotNumber(this.form.showOrder)){
					this.$message.error("权重必须为数字");
					return;
				}
				this.uploadForm.photo = this.fileList[0].url
				this.uploadForm.showOrder  = this.form.showOrder;
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.insertBanner,
					method: 'post',
					data: this.uploadForm
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						this.fileList = [];
						this.uploadForm.photo = [];
						this.uploadForm.showOrder = 0;
						
						this.form.showOrder = 0
						this.getAllCategory();
						this.getAllCoverList()
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			deleteGoodPropRow(val) {
               this.fullscreenLoading = true;
               let param={
               	   bannerId: val.uid,
               }
               axios({
               	url: POSTURL.deleteBanner,
               	method: 'post',
               	data: param
               }).then(response => {
               	this.fullscreenLoading = false;
               	if (response.data.code === 100) {
               		 this.getAllCoverList()
               	} else {
               		this.$message.error(response.data.message);
               	}
               }).catch(error => {
               	this.fullscreenLoading = false;
               	console.log(error);
               	this.$message.error('服务器出错!');
               })
			},
			//获取所有分类
			getAllCategory: function() {
				this.fullscreenLoading = true;
				let shopId = getShopId();
				axios({
					url: POSTURL.getAllCategory,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						for (let i = 0; i < response.data.result.length; i++) {
							let obj = response.data.result[i]
							let category = new Object();
							category.value = obj.name + "+" + obj.uid;
							category.label = obj.name;
							category.children = [];
							for (let j = 0; j < obj.secondary.length; j++) {
								let data = obj.secondary[j]
								let secondCategory = new Object();
								secondCategory.value = data.name + "+" + data.uid
								secondCategory.label = data.name
								category.children.push(secondCategory)
							}
							this.options.push(category)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			getAllCoverList(){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.selectAllBanner,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.goodPicTable  = response.data.result;
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			handleChange(data) {
				let s = []
				for (let i = 0; i < data.length; i++) {
					let obj = data[i];
					s.push(obj.split("+")[0])
					if (i === 1) {
						this.uploadForm.categoryId = obj.split("+")[1]
					}
				}
				this.value = s.join('>')

			},
		}
	})
})
